package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.oomall.core.model.VoObject;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.JacksonUtil;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.model.vo.AddressVo;
import cn.edu.xmu.oomall.customer.service.AddressService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:12:54
 */
@RestController
@RequestMapping(value = "", produces = "application/json;charset=UTF-8")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private HttpServletResponse httpServletResponse;

    @Audit
    @PostMapping("/addresses")
    public Object addNewAddress(@LoginUser Long creatorId,
                                @LoginName String creatorName,
                                @Validated @RequestBody AddressVo addressVo,
                                BindingResult bindingResult) {
        var res = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (res != null) {
            return res;
        }
        //System.out.println("Here in Controller");
        ReturnObject returnObject = addressService.addNewAddress(creatorId, creatorName, addressVo);
        return Common.decorateReturnObject(returnObject);
    }

    @Audit
    @GetMapping("/addresses")
    public Object getAllAddresses(@LoginUser Long customerId) {
        ReturnObject ret = addressService.getAllAddresses(customerId);
        return Common.decorateReturnObject(ret);
    }

    @Audit
    @PutMapping("/addresses/{id}/default")
    public Object setAddressDefaultById(@LoginUser Long customerId, @LoginName String customerName, @PathVariable Long id) {
        ReturnObject ret = addressService.setAddressDefault(customerId, customerName, id);
        return Common.decorateReturnObject(ret);
    }

    @Audit
    @PutMapping("/addresses/{id}")
    public Object modifyAddressById(@LoginUser Long customerId,
                                    @LoginName String customerName,
                                    @PathVariable Long id,
                                    @Validated @RequestBody AddressVo addressVo,
                                    BindingResult bindingResult) {
        var res = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (res != null) {
            return res;
        }
        ReturnObject ret = addressService.modifyAddress(customerId, customerName, id, addressVo);
        return Common.decorateReturnObject(ret);
    }

    @Audit
    @DeleteMapping("/addresses/{id}")
    public Object deleteAddressById(@LoginUser Long customerId, @PathVariable Long id) {
        ReturnObject ret = addressService.deleteAddress(customerId, id);
        return Common.decorateReturnObject(ret);
    }
}
